/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package alexpaz.clasesbase;

import java.util.ArrayList;
import javax.swing.JComponent;

/**
 *
 * @author Alex
 */
public class PnlMntto extends javax.swing.JPanel {

    private EstadoPanel estadoPanel = EstadoPanel.inicial;
    private ArrayList<JComponent> listaDeComponentesEditables = new ArrayList<>();
    private ArrayList<JComponent> listaDeComponentesParaBusqueda = new ArrayList<>();
    private AccionMntto accionMnttoRegistrar;
    private AccionMntto accionMnttoEliminar;
    private AccionMntto accionMnttoSalir;
    private AccionMntto accionMnttoBuscar;
    private AccionMntto accionMnttoModificar;

    /**
     * Permite el registro de un componente en especial JTextFields para ser
     * desactivados o activados durante los mantenimientos
     *
     * @param componente JComponent a anexar a la lista de componentes editables
     */
    public void registrarComponenteEditable(JComponent componente) {
        listaDeComponentesEditables.add(componente);
    }

    public void registrarComponenteParaBusqueda(JComponent componente) {
        listaDeComponentesParaBusqueda.add(componente);
    }

    /**
     * Habilita o deshabilita los componentes que hayan sido registrados usando
     * el método registrarComponenteEditable()
     *
     * @param habilitado boolean, indica habilitar (true) o deshabilitar (false)
     */
    public void habilitarComponentesEditables(boolean habilitado) {
        for (JComponent componente : listaDeComponentesEditables) {
            componente.setEnabled(habilitado);
        }
        if (listaDeComponentesEditables.size() > 0) {
            listaDeComponentesEditables.get(0).grabFocus();
        }
    }

    public void habilitarComponentesParaBusqueda(boolean habilitado) {
        for (JComponent componente : listaDeComponentesParaBusqueda) {
            componente.setEnabled(habilitado);
        }
    }

    /**
     * Creates new form PnlMntto
     */
    public PnlMntto() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnNuevo = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/nuevo.png"))); // NOI18N
        btnNuevo.setMnemonic('N');
        btnNuevo.setText("Nuevo");
        btnNuevo.setPreferredSize(new java.awt.Dimension(125, 45));
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/buscar.png"))); // NOI18N
        btnBuscar.setMnemonic('B');
        btnBuscar.setText("Buscar");
        btnBuscar.setPreferredSize(new java.awt.Dimension(125, 45));
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/salir.png"))); // NOI18N
        btnSalir.setMnemonic('S');
        btnSalir.setText("Salir");
        btnSalir.setPreferredSize(new java.awt.Dimension(125, 45));
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/eliminar.png"))); // NOI18N
        btnEliminar.setMnemonic('E');
        btnEliminar.setText("Eliminar");
        btnEliminar.setEnabled(false);
        btnEliminar.setPreferredSize(new java.awt.Dimension(125, 45));
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(btnNuevo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnBuscar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        if (estadoPanel == EstadoPanel.inicial) {
            ponerEstado(EstadoPanel.nuevo);
        } else if (estadoPanel == EstadoPanel.nuevo) {
            accionMnttoRegistrar.realizarAccionMntto();
            ponerEstado(EstadoPanel.inicial);
            habilitarComponentesEditables(false);
        } else if (estadoPanel == EstadoPanel.modificaOElimina) {
            ponerEstado(EstadoPanel.modificar);
            habilitarComponentesEditables(true);
        } else if (estadoPanel == EstadoPanel.modificar) {
            accionMnttoModificar.realizarAccionMntto();
            ponerEstado(EstadoPanel.inicial);
            habilitarComponentesEditables(false);
        }
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        if (estadoPanel == EstadoPanel.inicial) {
            ponerEstado(EstadoPanel.buscar);
            habilitarComponentesEditables(false);
            habilitarComponentesParaBusqueda(true);
        } else if (estadoPanel == EstadoPanel.buscar) {
            accionMnttoBuscar.realizarAccionMntto();
            if (estadoPanel == EstadoPanel.buscar) {
                ponerEstado(EstadoPanel.modificaOElimina);
                habilitarComponentesEditables(false);
            }
        }
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if (estadoPanel == EstadoPanel.modificaOElimina) {
            accionMnttoEliminar.realizarAccionMntto();
            ponerEstado(EstadoPanel.inicial);
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        accionMnttoSalir.realizarAccionMntto();
    }//GEN-LAST:event_btnSalirActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnSalir;
    // End of variables declaration//GEN-END:variables

    public void addAccionMnttoRegistrarDTO(AccionMntto accionMntto) {
        accionMnttoRegistrar = accionMntto;
    }

    public void addAccionMnttoEliminarDTO(AccionMntto accionMntto) {
        accionMnttoEliminar = accionMntto;
    }

    public void addAccionMnttoSalir(AccionMntto accionMntto) {
        accionMnttoSalir = accionMntto;
    }

    public void addAccionMnttoBuscarDTO(AccionMntto accionMntto) {
        accionMnttoBuscar = accionMntto;
    }
    
    public void addAccionMnttoModificarDTO (AccionMntto accionMntto) {
        accionMnttoModificar = accionMntto;
    }

    public void reset() {
        ponerEstado(EstadoPanel.inicial);
    }

    private enum EstadoPanel {

        inicial,
        nuevo,
        buscar,
        modificaOElimina,
        modificar
    }

    private void ponerEstado(EstadoPanel estado) {
        estadoPanel = estado;
        switch (estado) {
            case inicial:
                btnNuevo.setEnabled(true);
                btnNuevo.setText("Nuevo");
                btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/nuevo.png")));
                btnBuscar.setEnabled(true);
                btnBuscar.setText("Buscar");
                btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/buscar.png")));
                btnEliminar.setEnabled(false);
                habilitarComponentesEditables(false);
                break;
            case nuevo:
                btnNuevo.setEnabled(true);
                btnNuevo.setText("Guardar");
                btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/grabar.png")));
                btnBuscar.setEnabled(false);
                btnEliminar.setEnabled(false);
                habilitarComponentesEditables(true);
                break;
            case buscar:
                btnNuevo.setEnabled(false);
                btnBuscar.setEnabled(true);
                btnBuscar.setText("Ok");
                btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/ok.png")));
                btnEliminar.setEnabled(false);
                break;
            case modificaOElimina:
                btnNuevo.setEnabled(true);
                btnNuevo.setText("Modificar");
                btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/grabar.png")));
                btnBuscar.setEnabled(false);
                btnEliminar.setEnabled(true);
                break;
            case modificar:
                btnNuevo.setEnabled(true);
                btnNuevo.setText("Guardar");
                btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alexpaz/clasesbase/iconos/grabar.png")));
                btnBuscar.setEnabled(false);
                btnEliminar.setEnabled(false);
                break;
        }
    }
}
